package org.darcy.controller.check;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.darcy.entity.CheckReasonDetail;
import org.darcy.framework.model.PageData;
import org.darcy.framework.model.PageParam;
import org.darcy.framework.model.R;
import org.darcy.service.business.BusinessClientService;
import org.darcy.service.check.CheckReasonDetailService;
import org.darcy.service.check.CheckReasonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.excel.EasyExcel;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;

@Tag(name = "CheckReasonDetail接口：委托依据详情")
@RestController
@RequestMapping(value = "/CheckReasonDetail")
@Slf4j
public class CheckReasonDetailController {

	private final String info_prefix = "[委托依据详情模块]";

	@Autowired
	private CheckReasonDetailService checkReasonDetailService;

	@Autowired
	private CheckReasonService CheckReasonService;

	@Autowired
	private BusinessClientService BusinessClientService;

	@Operation(summary = "委托依据详情分页列表")
	@PostMapping("list")
	public R list(@RequestBody PageParam<CheckReasonDetail> param) {
		log.debug("{}-列表数据", info_prefix);
		PageData<CheckReasonDetail> data = checkReasonDetailService.page(param);
		return R.ok().data(data);
	}

	@Operation(summary = "委托依据详情详情")
	@GetMapping("/view/{id}")
	public R view(@PathVariable Integer id) {
		log.debug("{}-查看委托依据详情信息", info_prefix);
		CheckReasonDetail data = checkReasonDetailService.getById(id);
		return R.ok().data(data);
	}

	@Operation(summary = "新增委托依据详情")
	@PostMapping("add")
	public R add(@RequestBody CheckReasonDetail record) {
		log.debug("{}-新增CheckReasonDetail: {}", info_prefix, record);
		checkReasonDetailService.insert(record);
		return R.ok("新增成功").data(record);
	}

	@Operation(summary = "根据ID删除委托依据详情")
	@PostMapping("/del/{id}")
	public R del(@PathVariable Integer id) {
		log.debug("{}-根据ID删除", info_prefix, id);
		checkReasonDetailService.delete(id);
		return R.ok("删除成功");
	}

	@Operation(summary = "更新委托依据详情")
	@PostMapping("update")
	public R update(@RequestBody CheckReasonDetail record) {
		log.debug("{}-保存信息: {}", info_prefix, record);
		checkReasonDetailService.update(record);
		return R.ok("更新成功");
	}

	@Operation(summary = "查看委托依据详情属性")
	@GetMapping("/model")
	public CheckReasonDetail model() {
		return null;
	}

	@Operation(summary = "委托依据详情列表")
	@GetMapping("/getDetailList/{reasonId}")
	public R getDetailList(@PathVariable Integer reasonId) {
		List<CheckReasonDetail> data = checkReasonDetailService.getList(reasonId);
		return R.ok().data(data);
	}

	@Operation(summary = "委托依据详情导出")
	@GetMapping("export/{reasonId}")
	public void export(@PathVariable Integer reasonId, HttpServletResponse response) throws IOException {
		List<CheckReasonDetail> data = checkReasonDetailService.getList(reasonId);
		String clientName = BusinessClientService.getById(CheckReasonService.getById(reasonId).getClientId()).getName();
		// 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
		response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
		response.setCharacterEncoding("utf-8");
		// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
		String fileName = URLEncoder.encode("委托依据详情", "UTF-8").replaceAll("\\+", "%20");
		response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
		EasyExcel.write(response.getOutputStream(), CheckReasonDetail.class).sheet(clientName).doWrite(data);
	}

}
